package org.sean.framework.canary.feign;

import org.sean.framework.logging.Logger;
import org.sean.framework.util.StringUtil;
import org.sean.framework.web.holder.RequestHolder;
import org.slf4j.MDC;

import java.util.Map;

/**
 * 灰度 Holder
 *
 * @author xielei
 */
public class CanaryInfoHolder {

    private CanaryInfoHolder() {
    }

    public static void handle(Map<String, String> headers, String traceId, String spanId) {
        RequestHolder.getHeaders().putAll(headers);
        if (StringUtil.isNotEmpty(traceId)) {
            RequestHolder.getHeaders().put(Logger.KEY_TRACE, traceId);
            MDC.put(Logger.KEY_TRACE, traceId);
        }
        if (StringUtil.isNotEmpty(spanId)) {
            RequestHolder.getHeaders().put(Logger.KEY_SPAN, spanId);
            MDC.put(Logger.KEY_SPAN, spanId);
        }

    }

    /**
     * 线程池调用
     */
    public static void cleanHeaders() {
        RequestHolder.getHeaders().clear();
        MDC.clear();
    }

}
